Skip to content

fix(solver): resolve extra dependencies missing from a reused package's requires - #10987

Merged
radoering merged 4 commits into
python-poetry:mainfrom
nabsei:fix-vcs-extras-not-resolved-from-locked-package-10314
Aug 1, 2026
Merged

radoering merged 4 commits into
python-poetry:mainfrom
nabsei:fix-vcs-extras-not-resolved-from-locked-package-10314

Conversation

@nabsei

@nabsei nabsei commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

Pull Request Check List

Resolves: #10314

  • Added tests for changed code.
  • Updated documentation for changed code. (not applicable — internal solver behavior, no user-facing docs to update)

What

Editing pyproject.toml to add an extra to a VCS dependency (eg. changing sqlalchemy @ git+... to sqlalchemy[postgresql] @ git+...) and running poetry lock silently drops the extra's own dependencies (eg. psycopg), unless the same change is instead made via poetry add.

Why

Provider.complete_package() looks up a package's optional/in-extras dependencies by matching names found in package.extras against Dependency objects present in package.requires. That works for a freshly-inspected package, but a package reused from the lock file (Locker._get_locked_package()) may have a requires list pruned down to whatever extras were active during an earlier resolution, while package.extras always keeps the full mapping. When the matching Dependency object isn't in requires anymore, the name is still recognized as needed but nothing gets added to satisfy it.

The fix falls back to the Dependency objects in package.extras for names that are entirely absent from requires. Names that are present but excluded for another reason (eg. a marker that doesn't match the current environment) are left alone, since that exclusion is deliberate — a second regression test covers this to make sure the fallback doesn't override legitimate marker-based exclusions.

Verification

  • Reproduced the bug against a real poetry installed from source, using an actual git clone of SQLAlchemy as the VCS dependency.
  • Confirmed the fix resolves the reported case, and that removing the extra / re-locking with no changes both still behave correctly (stable, no regressions).
  • Added two targeted regression tests in tests/puzzle/test_provider.py.
  • Full test suite: 1104 passed; the only failures are 3 pre-existing ones unrelated to this change (confirmed identical on unmodified main: an LFS test needing network access, and a Python 3.14/embedded-pip environment issue).
  • ruff check, ruff format --check, and mypy all clean on the changed files.

Disclosure: this PR was prepared with the assistance of Claude Code (Anthropic). The investigation, root-cause diagnosis, fix, and verification steps described above were done and checked by me.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • In the fallback path where extra dependencies missing from requires are appended, the marker/python constraint filtering applied in the main requires loop is bypassed; consider reusing that filtering logic so that environment-specific markers are still respected for dependencies coming only from extras.
  • The extra_dependency_by_name and names_in_requires lookups key solely on dep.name; if there are cases where names are not canonicalized consistently between extras and requires, consider normalizing (e.g., with canonicalize_name) in both places to avoid subtle mismatches.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In the fallback path where extra dependencies missing from `requires` are appended, the marker/python constraint filtering applied in the main `requires` loop is bypassed; consider reusing that filtering logic so that environment-specific markers are still respected for dependencies coming only from `extras`.
- The `extra_dependency_by_name` and `names_in_requires` lookups key solely on `dep.name`; if there are cases where names are not canonicalized consistently between `extras` and `requires`, consider normalizing (e.g., with `canonicalize_name`) in both places to avoid subtle mismatches.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread src/poetry/puzzle/provider.py Resolved
nabsei and others added 3 commits August 1, 2026 10:20
…'s requires

Editing pyproject.toml to add an extra to a VCS dependency (eg. changing
"sqlalchemy @ git+..." to "sqlalchemy[postgresql] @ git+...") and running
`poetry lock` silently drops the extra's own dependencies (eg. psycopg),
unless the same change is instead made via `poetry add`.

Provider.complete_package() looks up a package's optional/in-extras
dependencies by matching names found in `package.extras` against Dependency
objects present in `package.requires`. That works for a freshly-inspected
package, but a package reused from the lock file (Locker._get_locked_package())
may have a `requires` pruned down to whatever extras were active during an
earlier resolution, while `package.extras` always keeps the full mapping.
When the matching Dependency object isn't in `requires` anymore, the name is
still recognized as needed but nothing gets added to satisfy it.

Fall back to the Dependency objects in `package.extras` for names that are
entirely absent from `requires`. Names that are present but excluded for
another reason (eg. a marker that doesn't match the current environment)
are left alone, since that exclusion is deliberate.

Fixes python-poetry#10314

Co-authored-by: Claude <noreply@anthropic.com>
…first

extra_dependency_by_name used setdefault(), so if an extra listed
several same-named dependencies distinguished only by marker (eg. a
platform-restricted variant per sys_platform), only the first one
seen was kept. If none of them were present in a package's (pruned,
reused-from-lock) requires, the fallback added only that first one
back, silently dropping the other marker branch.

Store a list per name instead and restore all of them. Added a
solver-level regression test mirroring
test_solver_resolves_duplicate_dependencies_with_restricted_extras,
but with both same-named deps only in extras (not requires), which
fails on the old setdefault-based fallback.
@radoering
radoering force-pushed the fix-vcs-extras-not-resolved-from-locked-package-10314 branch from d6ab4cb to 5c4994d Compare August 1, 2026 09:44
@radoering

Copy link
Copy Markdown
Member

@sourcery-ai review

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="tests/puzzle/test_provider.py" line_range="919-927" />
<code_context>
+    assert {r.name for r in requires} == {"a", "b"}
+
+
+def test_complete_package_respects_marker_exclusion_over_missing_requires(
+    root: ProjectPackage, repository: Repository, pool: RepositoryPool
+) -> None:
+    """
+    A dependency present in `requires` but excluded by a marker mismatch must
+    stay excluded, even though it's also absent from the filtered result (same
+    as a genuinely-missing-from-requires dependency would be).
+    """
+    root = ProjectPackage("root", "1.2.3")
+    root.python_versions = "^3.9"
+    locked_package = Package("A", "1.0", source_type="url", source_url=SOME_URL)
</code_context>
<issue_to_address>
**suggestion:** Avoid shadowing the `root` fixture in `test_complete_package_respects_marker_exclusion_over_missing_requires`

This test reassigns `root = ProjectPackage("root", "1.2.3")`, so the `root` fixture parameter is never used. That hurts readability and consistency with other tests that rely on the shared fixture. Please either remove `root` from the parameters and keep the local instance, or update the fixture itself (e.g., set `python_versions` on the injected `root`).

Suggested implementation:

```python
def test_complete_package_respects_marker_exclusion_over_missing_requires(
    root: ProjectPackage, repository: Repository, pool: RepositoryPool
) -> None:
    """
    A dependency present in `requires` but excluded by a marker mismatch must
    stay excluded, even though it's also absent from the filtered result (same
    as a genuinely-missing-from-requires dependency would be).
    """
    root.python_versions = "^3.9"
    locked_package = Package("A", "1.0", source_type="url", source_url=SOME_URL)

```

The rest of the body of `test_complete_package_respects_marker_exclusion_over_missing_requires` (the `provider = Provider(root, ...)` and subsequent assertions) can stay as-is. This change ensures the injected `root` fixture is used instead of shadowed, while still configuring its `python_versions` for the test.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread tests/puzzle/test_provider.py Resolved
@radoering

Copy link
Copy Markdown
Member

I made the fix more targeted and the test a bit more realistic since this is only an issue of direct origin dependencies. Further, the subsequent filtering is not circumvented anymore.

@radoering
radoering enabled auto-merge (squash) August 1, 2026 09:55
@radoering
radoering merged commit 62ffee2 into python-poetry:main Aug 1, 2026
52 checks passed
@github-actions

github-actions Bot commented Sep 1, 2026

Copy link
Copy Markdown

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Sep 1, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optional dependencies for git dependencies aren't resolved with poetry lock

3 participants